package com.distortiongames.level27.generation
{
	import flash.utils.Dictionary;
	
	/**
	 * ...
	 * @author Jefferson Wagner
	 */
	public class KeywordRepository
	{
		public var shades:Dictionary;
		public var primaryWords:Dictionary;
		public var secondaryWords:Dictionary;
		public var tertiaryWords:Dictionary;
		private var shadeCount:int;
		
		public function KeywordRepository(xml:XML)
		{
			shades = new Dictionary();
			primaryWords = new Dictionary();
			secondaryWords = new Dictionary();
			tertiaryWords = new Dictionary();
			processShades(xml.elements("shades").elements());
			processKeywords(xml.elements("primary").elements(), primaryWords);
			processKeywords(xml.elements("secondary").elements(), secondaryWords);
			processKeywords(xml.elements("tertiary").elements(), tertiaryWords);
		}
		
		public function getRandomShade():Shade
		{
			var random:int = Math.random() * countKeys(shades);
			var index:int = 0;
			for each (var value:Shade in shades)
			{
				if (index == random)
					return value;
				index++;
			}
			return null;
		}
		
		public function getRandomPrimary():Keyword
		{
			var random:int = Math.random() * countKeys(primaryWords);
			var index:int = 0;
			for each (var value:Keyword in primaryWords)
			{
				if (index == random)
					return value;
				index++;
			}
			return null;
		}
		
		public function getRandomSecondary():Keyword
		{
			var random:int = Math.random() * countKeys(secondaryWords);
			var index:int = 0;
			for each (var value:Keyword in secondaryWords)
			{
				if (index == random)
					return value;
				index++;
			}
			return null;
		}
		
		public function getRandomTertiary():Keyword
		{
			var random:int = Math.random() * countKeys(tertiaryWords);
			var index:int = 0;
			for each (var value:Keyword in tertiaryWords)
			{
				if (index == random)
					return value;
				index++;
			}
			return null;
		}
		
		private function countKeys(dictionary:Dictionary):int
		{
			var length:int = 0;
			for (var key:*in dictionary)
			{
				length++;
			}
			return length;
		}
		
		private function processShades(list:XMLList):void
		{
			for each (var shadeInfo:XML in list)
			{
				var shade:Shade = new Shade();
				shade.level = shadeInfo.attribute("level");
				shade.name = shadeInfo.attribute("name");
				shade.element = Element.fromString(shadeInfo.attribute("element"));
				shades[shade.name] = shade;
				processNode(shadeInfo.elements(), shade);
			}
		}
		
		private function processKeywords(list:XMLList, dictionary:Dictionary):void
		{
			for each (var keywordInfo:XML in list)
			{
				var keyword:Keyword = new Keyword();
				keyword.name = keywordInfo.attribute("name");
				processNode(keywordInfo.elements(), keyword);
				dictionary[keyword.name] = keyword;
			}
		}
		
		private function processNode(xml:XMLList, keyword:Keyword):void
		{
			if (xml.length() == 1)
			{
				var prop:String = xml.name();
				var value:Number = xml.toString();
				keyword.properties[prop] = value;
			}
			else if (xml.length() > 1)
			{
				for each (var property:XML in xml)
				{
					prop = property.name();
					value = property.toString();
					keyword.properties[prop] = value;
				}
			}
		
		}
	}

}